from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_socketio import SocketIO
from flask_migrate import Migrate
from werkzeug.routing import BaseConverter

class RegexConverter(BaseConverter):
    def __init__(self, url_map,*items):
        super(RegexConverter,self).__init__(url_map)
        self.regex = items[0]


db = SQLAlchemy()
socketio = SocketIO()
migrate = Migrate()

def create_app(config):
    app = Flask(__name__,static_folder="../dist",static_url_path="")
    app.config.from_object(config)
    db.init_app(app)
    socketio.init_app(app,cors_allowed_origins="*")
    migrate.init_app(app,db)

    # 使路由匹配支持正则表达式
    app.url_map.converters['re'] = RegexConverter

    # 注册蓝图
    from app.api import bp as api_bp
    app.register_blueprint(api_bp,url_prefix="/api")

    from app.routes import bp as main_bp
    app.register_blueprint(main_bp,url_prefix='/xss')

    db.create_all(app=app)
    return app


from app.modules import *